package com.cn.ant.modules.score.web;

import com.cn.ant.common.config.Global;
import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.DateUtils;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.utils.excel.ExportExcel;
import com.cn.ant.common.utils.excel.ImportExcel;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.score.entity.YfdInfo;
import com.cn.ant.modules.score.service.YfdInfoService;
import com.cn.ant.modules.sys.entity.Area;
import com.cn.ant.modules.sys.service.AreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一分段信息Controller
 * @author AntDream
 * @version 2015-08-17
 */
@Controller
@RequestMapping(value = "${adminPath}/score/yfdInfo")
public class YfdInfoController extends BaseController {

	@Autowired
	private YfdInfoService yfdInfoService;
	@Autowired
	private AreaService areaService;
	
	@ModelAttribute
	public YfdInfo get(@RequestParam(required=false) String id) {
		YfdInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = yfdInfoService.get(id);
		}
		if (entity == null){
			entity = new YfdInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("score:yfdInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(YfdInfo yfdInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<YfdInfo> page = yfdInfoService.findPage(new Page<YfdInfo>(request, response), yfdInfo);
		//查询省的信息
		Map<String, String> params = new HashMap<>();
		params.put("type", "2");
		List<Area> provinceList = areaService.query(params);
		model.addAttribute("provinceList", provinceList);
		model.addAttribute("page", page);
		return "modules/score/yfdInfoList";
	}

	@RequiresPermissions("score:yfdInfo:view")
	@RequestMapping(value = "form")
	public String form(YfdInfo yfdInfo, Model model) {
		model.addAttribute("yfdInfo", yfdInfo);
		//查询省的信息
		Map<String, String> params = new HashMap<>();
		params.put("type", "2");
		List<Area> provinceList = areaService.query(params);
		model.addAttribute("provinceList", provinceList);
		return "modules/score/yfdInfoForm";
	}

	@RequiresPermissions("score:yfdInfo:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(YfdInfo yfdInfo, HttpServletRequest request,Model model) {
		AjaxJson jo = new AjaxJson();
		try {
			if (!beanValidator(model, yfdInfo)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
			} else {
			    yfdInfoService.save(yfdInfo);
			}
			msg = "保存一分段信息成功";
            jo.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("score:yfdInfo:edit")
	@RequestMapping(value = "delete")
    @ResponseBody
	public AjaxJson delete(YfdInfo yfdInfo, RedirectAttributes redirectAttributes) {
        AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(yfdInfo.getId())) {
            yfdInfoService.delete(yfdInfo);
            jo.setMsg("删除一分段信息成功");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

    /**
     * 导出
     * @param yfdInfo
     * @param request
     * @param response
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("score:yfdInfo:edit")
    @RequestMapping(value = "export", method= RequestMethod.POST)
    public String exportFle(YfdInfo yfdInfo,HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
        try {
            String fileName = "一分段数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Map<String, Object> params = new HashMap<String, Object>();
            Page<YfdInfo> page = yfdInfoService.findPage(new Page<YfdInfo>(request, response, -1), yfdInfo);
            new ExportExcel("一分段数据", YfdInfo.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出一分段数据失败！失败信息：" + e.getMessage());
            e.printStackTrace();
        }
        return "redirect:"+Global.getAdminPath()+"/score/yfdInfo/?repage";
    }

    @RequiresPermissions("score:yfdInfo:edit")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "一分段数据导入模板.xlsx";
            Page<YfdInfo> page = yfdInfoService.findPage(new Page<YfdInfo>(1, 1), new YfdInfo());
            new ExportExcel("一分段数据", YfdInfo.class, 2).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
            e.printStackTrace();
        }
        return "redirect:"+Global.getAdminPath()+"/score/yfdInfo/?repage";
    }

    /**
     * 导入专业信息
     * @param file
     * @param request
     * @param redirectAttributes
     * @param model
     * @return
     */
    @RequiresPermissions("score:yfdInfo:edit")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, HttpServletRequest request,RedirectAttributes redirectAttributes,Model model) {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<YfdInfo> list = ei.getDataList(YfdInfo.class);
            yfdInfoService.batchInsert(list);
            logger.info("导入一分段数据成功");
            addMessage(redirectAttributes, "导入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:"+Global.getAdminPath()+"/score/yfdInfo/?repage";
    }


}